package com.lmaye.lr.common.context;

import java.util.Objects;

/**
 * -- Web Application 上下文
 *
 * @author lmay.Zhou
 * @date 2019/12/6 18:02 星期五
 * @email lmay@lmaye.com
 */
public class BaseContext {
    private static ThreadLocal<BaseInfo> threadLocal = new ThreadLocal<>();

    /**
     * 获取请求信息
     *
     * @return BaseInfo
     */
    public static BaseInfo getBaseInfo() {
        BaseInfo baseInfo = threadLocal.get();
        if(Objects.isNull(baseInfo)) {
            baseInfo = BaseInfo.builder().build();
            setBaseInfo(baseInfo);
        }
        return baseInfo;
    }

    /**
     * 设置请求信息
     *
     * @param baseInfo 请求信息
     */
    public static void setBaseInfo(BaseInfo baseInfo) {
        threadLocal.set(baseInfo);
    }

    /**
     * 清除请求信息
     */
    public static void clear() {
        threadLocal.remove();
    }
}
